package stateless;

import entity.Book;
import java.sql.*;
import java.util.*;
import javax.annotation.*;
import javax.ejb.*;
import session.BookFacadeLocal;

@Stateless
public class BookBean implements BookBeanLocal {
    @EJB
    private BookFacadeLocal bookFacade;
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf-8";
    static final String USER = "root";
    static final String PASS = "root";
    
    private Connection conn;
    private Statement stmt;
    
    public BookBean(){}
    
    @PostConstruct
    @PostActivate
    public void openConnection(){
        try{
            Class.forName(JDBC_DRIVER);                             // 注册 JDBC 驱动
            conn = DriverManager.getConnection(DB_URL, USER, PASS); // 打开链接
            stmt = conn.createStatement();                          //构造查询驱动
        }
        catch (SQLException se) {                   // 处理 JDBC 错误 
            System.out.println("BookBean openCoonenction raise() Exception:"+se.getMessage());
        }
        catch (ClassNotFoundException e) {          // 处理 Class.forName 错误
            System.out.println("BookBean openCoonenction raise() Exception"+e.getMessage());
        }      
    }
    
    @PrePassivate
    @PreDestroy
    public void cleanup(){
        try {   
            if (stmt != null) stmt.close();
        }catch (SQLException se) { System.out.println("BookBean cleanup() raise Exception"+se.getMessage());}
        try {
            if (conn != null) conn.close();
        }catch (SQLException se) {System.out.println("BookBean cleanup() raise Exception"+se.getMessage()); }
    }
    
    
    
    @Override
    public String getAString() {
        return "BookBean test Successful.";
    }

    @Override
    public ArrayList<ArrayList<String>> getBook(String bookName) {
        List<Book> books = (List)this.bookFacade.findByNameLike(bookName);
        ArrayList<ArrayList<String>> result = new ArrayList<>();
        
        for(Book b:books){
            ArrayList<String> list = new ArrayList<>();
            list.add(String.valueOf(b.getId()));
            list.add(b.getName());
            list.add(String.valueOf(b.getCategory()));
            list.add(String.valueOf(b.getPrice()));
            list.add(b.getAuthor());
            list.add(b.getPress());
            result.add(list);
        }
        
        /*String sql = "select * from book where name like \"%" + bookName + "%\"" ;
        ArrayList<ArrayList<String>> result = new ArrayList<>();
        
        
        try {
            ResultSet rs= stmt.executeQuery(sql);
            while(rs.next()){
                ArrayList<String> list = new ArrayList<>();
                for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++){
                    list.add(rs.getString(i));
                }
                result.add(list);
            }
        } catch (SQLException ex) {
            System.out.println("BookBean getBook() raise Exception"+ex.getMessage());
        }*/
        
        return result;
    }

    @Override
    public ArrayList<String> findBook(int id) {
        ArrayList<String> result = new ArrayList<>();
        Book book = (Book)this.bookFacade.find(id);
        result.add(String.valueOf(book.getId()));
        result.add(book.getName());
        result.add(String.valueOf(book.getCategory()));
        result.add(String.valueOf(book.getPrice()));
        result.add(book.getAuthor());
        result.add(book.getPress());
       
        
        /*String sql = "select * from book where id =" + id ;
        ArrayList<String> result = new ArrayList<>();
        
        
        try {
            ResultSet rs= stmt.executeQuery(sql);
            while(rs.next()){
                for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++){
                    result.add(rs.getString(i));
                }
            }
        } catch (SQLException ex) {
            System.out.println("BookBean findBook() raise Exception"+ex.getMessage());
        }*/

        return result;
    }
}
